package services;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Random;

import models.DrCourt;
import models.DrCourtExample;
import models.DrMicrophone;
import models.DrMicrophoneExample;
import models.SystemLog;
import models.SystemOperator;
import models.mapper.DrCourtMapper;
import models.mapper.DrMicrophoneMapper;
import models.mapper.SystemLogMapper;

import org.apache.ibatis.session.SqlSession;

import play.libs.Codec;

import com.aton.db.SessionFactory;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;

import controllers.MicroPhoneController;
import utils.BizConstants;
import utils.PageUtil;
import vos.DrCourtVo;
import vos.Node;

/**
 * 法庭服务类
 * @author wangshaoming
 *
 */
public class DrCourtService {
	
	/**
	 * 获取下拉框
	 * @return
	 */
	public static List<Node> getCombobox() {
		// TODO Auto-generated method stub
		SqlSession ss=SessionFactory.getSqlSession();
		try{
			DrCourtMapper mapper=ss.getMapper(DrCourtMapper.class);
			return mapper.getCombobox();
		}finally{
			ss.close();
		}
	}
	/**
	 * 根据id获取法庭
	 * @param courtid 法庭id
	 * @return
	 */
	public static DrCourt getById(String courtid) {
		// TODO Auto-generated method stub
		SqlSession ss=SessionFactory.getSqlSession();
		
		try{
			DrCourtMapper mapper=ss.getMapper(DrCourtMapper.class);
			return mapper.selectByPrimaryKey(courtid);
		}finally{
			ss.close();
		}
	}
	public static PageInfo<DrCourt> findCourt(int page, int rows, String sort,
			String order, DrCourtVo drCourtVo) {
		SqlSession ss=SessionFactory.getSqlSession();
		try{
			PageHelper.startPage(page, rows,PageUtil.joinSortFieldOrder(sort, order));
			DrCourtMapper mapper=ss.getMapper(DrCourtMapper.class);
			return new PageInfo<DrCourt>(mapper.findCourt(drCourtVo));
		}finally{
			ss.close();
		}
	}
	/**
	 * 添加法庭
	 * @param drCourt
	 * @param loginUser
	 * @param remoteAddress
	 * @param phones
	 * @return
	 */
	public static Boolean addCourt(DrCourt drCourt, SystemOperator loginUser,
			String remoteAddress, DrMicrophone[] phones) {
		SqlSession ss=SessionFactory.getSqlSession();
		try{
			drCourt.setCourtid(Codec.UUID());
			drCourt.setCreatetime(new Date());
			drCourt.setStatus(DrCourt.ENABLE);
			drCourt.setCreatename(loginUser.getOperatorid());
			DrCourtMapper mapper=ss.getMapper(DrCourtMapper.class);
			mapper.insert(drCourt);
			/**
			 * 插入麦克风
			 */
			DrMicrophoneMapper microMapper=ss.getMapper(DrMicrophoneMapper.class);
			for(DrMicrophone phone :phones){
				phone.setCreatename(loginUser.getOperatorid());
				phone.setCreatetime(new Date());
				phone.setStaffrole(phone.getMicroname());
				phone.setStatus(DrMicrophone.ENABLE);
				phone.setBlongcourt(drCourt.getCourtid());
				microMapper.insert(phone);
			}
			SystemLog log=new SystemLog(Codec.UUID(),loginUser.getOperatorid(),remoteAddress,"新增法庭数据","新增法庭数据",new Date());
			SystemLogMapper logMapper=ss.getMapper(SystemLogMapper.class);
			logMapper.insert(log);
			ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			ss.rollback();
			return false;
		}finally{
			ss.close();
		}
		return Boolean.TRUE;
	}
	/**
	 * 更新法庭和麦克风信息
	 * @param drCourt
	 * @param loginUser
	 * @param remoteAddress
	 * @param phones
	 * @return
	 */
	public static Boolean updateCourt(DrCourt drCourt, SystemOperator loginUser,
			String remoteAddress, DrMicrophone[] phones) {
		SqlSession ss=SessionFactory.getSqlSession();
		try{
			DrCourtMapper mapper=ss.getMapper(DrCourtMapper.class);
			mapper.updateByPrimaryKeySelective(drCourt);
			DrMicrophoneMapper microMapper=ss.getMapper(DrMicrophoneMapper.class);
			//删除现有麦克风，重新插入麦克风
			DrMicrophoneExample example=new DrMicrophoneExample();
			DrMicrophoneExample.Criteria c=example.createCriteria();
			c.andBlongcourtEqualTo(drCourt.getCourtid());
			microMapper.deleteByExample(example);
			for(DrMicrophone phone :phones){
				phone.setCreatename(loginUser.getOperatorid());
				phone.setCreatetime(new Date());
				phone.setStaffrole(phone.getMicroname());
				phone.setStatus(DrMicrophone.ENABLE);
				phone.setBlongcourt(drCourt.getCourtid());
				microMapper.insert(phone);
			}
			SystemLog log=new SystemLog(Codec.UUID(),loginUser.getOperatorid(),remoteAddress,"更新法庭数据","更新法庭数据",new Date());
			SystemLogMapper logMapper=ss.getMapper(SystemLogMapper.class);
			logMapper.insert(log);
		}catch(Exception e){
			e.printStackTrace();
			ss.rollback();
			return false;
		}finally{
			ss.close();
		}
		return Boolean.TRUE;
	}
	public static Boolean betchUpdateStatus(String ids, BigDecimal enable,
			SystemOperator loginUser, String remoteAddress) {
		SqlSession ss=SessionFactory.getSqlSession();
		try{
			DrCourtMapper mapper=ss.getMapper(DrCourtMapper.class);
			mapper.betchUpdateStatus(ids,enable);
			
			SystemLog log=new SystemLog(Codec.UUID(),loginUser.getOperatorid(),remoteAddress,"更新法庭数据","更新法庭数据",new Date());
			SystemLogMapper logMapper=ss.getMapper(SystemLogMapper.class);
			logMapper.insert(log);
		}finally{
			ss.close();
		}
		return Boolean.TRUE;
	}
	/**
	 * 根据名称获取法庭
	 * @param courtname
	 * @return
	 */
	public static DrCourt getByName(String courtname) {
		// TODO Auto-generated method stub
		SqlSession ss=SessionFactory.getSqlSession();
		try{
			DrCourtMapper mapper=ss.getMapper(DrCourtMapper.class);
			DrCourtExample example=new DrCourtExample();
			DrCourtExample.Criteria c=example.createCriteria();
			c.andCourtnameEqualTo(courtname);
			List<DrCourt> courts=mapper.selectByExample(example);
			if(courts!=null&&courts.size()>0){
				return courts.get(0);
			}
			return null;
		}finally{
			ss.close();
		}
	}
	/**
	 * 获取在序列中并且使用中的元素
	 * @param ids
	 * @return
	 */
	public static List<DrCourt> getInUseCournt(String ids) {
		// TODO Auto-generated method stub
		SqlSession ss=SessionFactory.getSqlSession();
		try{
			DrCourtMapper mapper=ss.getMapper(DrCourtMapper.class);
			return mapper.getInUseCournt(ids);
		}finally{
			ss.close();
		}
	}

}
